package com.quan.common.scheduler;

import java.util.Map;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.quan.system.entity.SystemJob;

import lombok.extern.slf4j.Slf4j;

/**
 * Quartz 定时任务工具类
 * 
 * @author Administrator
 *
 */
@Slf4j
@Component
public class SchedulerUtils {

	@Autowired
	private Scheduler scheduler;

	/**
	 * 新建定时任务
	 * 
	 * @param job
	 */
	public void newJob(SystemJob job) {
		log.info("==>【新建定时任务】任务名称：{}，任务组名称：{}，Cron表达式：{}，任务描述信息：{}，作业类：{}，作业参数：{}，任务状态：{}", job.getJobName(),
				job.getJobGroup(), job.getJobCron(), job.getJobDesc(), job.getJobClassPath(), job.getJobParams(),
				job.getJobStatus());

		try {
			Class<Job> jobClass = getJobClass(job.getJobClassPath());

			// 构建JOB信息
			JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(job.getJobName(), job.getJobGroup())
					.withDescription(job.getJobDesc()).build();

			// 触发时间点
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getJobCron());
			// 触发器
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger-" + job.getJobName(), job.getJobGroup())
					.startNow().withSchedule(cronScheduleBuilder).build();

			// 添加JobDataMap数据
			if (null != job.getJobDataMap()) {
				Map<String, Object> jobDataMap = job.getJobDataMap();
				for (String key : jobDataMap.keySet()) {
					jobDetail.getJobDataMap().put(key, jobDataMap.get(key));
				}
			}

			// 交由Scheduler安排触发
			scheduler.scheduleJob(jobDetail, trigger);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 暂停任务
	 * 
	 * @param jobName
	 * @param jobGroup
	 */
	public void pauseJob(String jobName, String jobGroup) {
		log.info("==>【暂停任务】任务名称：{}，任务组名称：{}", jobName, jobGroup);
		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			scheduler.pauseJob(jobKey);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 恢复任务
	 * 
	 * @param jobName
	 * @param jobGroup
	 */
	public void resumeJob(String jobName, String jobGroup) {
		log.info("==>【恢复任务】任务名称：{}，任务组名称：{}", jobName, jobGroup);
		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			scheduler.resumeJob(jobKey);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 触发任务
	 * 
	 * @param jobName
	 * @param jobGroup
	 */
	public void triggerJob(String jobName, String jobGroup) {
		log.info("==>【触发任务】任务名称：{}，任务组名称：{}", jobName, jobGroup);

		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			scheduler.triggerJob(jobKey);

		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除任务
	 * 
	 * @param jobName
	 * @param jobGroup
	 */
	public void deleteJob(String jobName, String jobGroup) {
		try {
			JobKey key = new JobKey(jobName, jobGroup);

			// 先暂停任务
			pauseJob(jobName, jobGroup);

			TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
			// 停止触发器
			scheduler.pauseTrigger(triggerKey);
			// 移除触发器
			scheduler.unscheduleJob(triggerKey);

			// 删除任务
			scheduler.deleteJob(key);
			log.info("==>【删除任务】任务名称：{}，任务组名称：{}", jobName, jobGroup);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "unchecked" })
	private Class<Job> getJobClass(String jobClassPath) throws ClassNotFoundException {
		return (Class<Job>) Class.forName(jobClassPath);
	}

}
